#!/usr/bin/env bash

. common.sh
function MySQLCommonYum() {
    yum install -y cmake
}
# 优化MySQL的配置
MySQLOptimize()
{
    if [[ ${MemTotal} -gt 1024 && ${MemTotal} -lt 2048 ]]; then
        sed -i "s#^key_buffer_size.*#key_buffer_size = 32M#" /etc/my.cnf
        sed -i "s#^table_open_cache.*#table_open_cache = 128#" /etc/my.cnf
        sed -i "s#^sort_buffer_size.*#sort_buffer_size = 768K#" /etc/my.cnf
        sed -i "s#^read_buffer_size.*#read_buffer_size = 768K#" /etc/my.cnf
        sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 8M#" /etc/my.cnf
        sed -i "s#^thread_cache_size.*#thread_cache_size = 16#" /etc/my.cnf
        sed -i "s#^query_cache_size.*#query_cache_size = 16M#" /etc/my.cnf
        sed -i "s#^tmp_table_size.*#tmp_table_size = 32M#" /etc/my.cnf
        sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 128M#" /etc/my.cnf
        sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 32M#" /etc/my.cnf
        sed -i "s#^performance_schema_max_table_instances.*#performance_schema_max_table_instances = 1000#" /etc/my.cnf
    elif [[ ${MemTotal} -ge 2048 && ${MemTotal} -lt 4096 ]]; then
        sed -i "s#^key_buffer_size.*#key_buffer_size = 64M#" /etc/my.cnf
        sed -i "s#^table_open_cache.*#table_open_cache = 256#" /etc/my.cnf
        sed -i "s#^sort_buffer_size.*#sort_buffer_size = 1M#" /etc/my.cnf
        sed -i "s#^read_buffer_size.*#read_buffer_size = 1M#" /etc/my.cnf
        sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 16M#" /etc/my.cnf
        sed -i "s#^thread_cache_size.*#thread_cache_size = 32#" /etc/my.cnf
        sed -i "s#^query_cache_size.*#query_cache_size = 32M#" /etc/my.cnf
        sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
        sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 256M#" /etc/my.cnf
        sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 64M#" /etc/my.cnf
        sed -i "s#^performance_schema_max_table_instances.*#performance_schema_max_table_instances = 2000#" /etc/my.cnf
    elif [[ ${MemTotal} -ge 4096 && ${MemTotal} -lt 8192 ]]; then
        sed -i "s#^key_buffer_size.*#key_buffer_size = 128M#" /etc/my.cnf
        sed -i "s#^table_open_cache.*#table_open_cache = 512#" /etc/my.cnf
        sed -i "s#^sort_buffer_size.*#sort_buffer_size = 2M#" /etc/my.cnf
        sed -i "s#^read_buffer_size.*#read_buffer_size = 2M#" /etc/my.cnf
        sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 32M#" /etc/my.cnf
        sed -i "s#^thread_cache_size.*#thread_cache_size = 64#" /etc/my.cnf
        sed -i "s#^query_cache_size.*#query_cache_size = 64M#" /etc/my.cnf
        sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
        sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 512M#" /etc/my.cnf
        sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 128M#" /etc/my.cnf
        sed -i "s#^performance_schema_max_table_instances.*#performance_schema_max_table_instances = 4000#" /etc/my.cnf
    elif [[ ${MemTotal} -ge 8192 && ${MemTotal} -lt 16384 ]]; then
        sed -i "s#^key_buffer_size.*#key_buffer_size = 256M#" /etc/my.cnf
        sed -i "s#^table_open_cache.*#table_open_cache = 1024#" /etc/my.cnf
        sed -i "s#^sort_buffer_size.*#sort_buffer_size = 4M#" /etc/my.cnf
        sed -i "s#^read_buffer_size.*#read_buffer_size = 4M#" /etc/my.cnf
        sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 64M#" /etc/my.cnf
        sed -i "s#^thread_cache_size.*#thread_cache_size = 128#" /etc/my.cnf
        sed -i "s#^query_cache_size.*#query_cache_size = 128M#" /etc/my.cnf
        sed -i "s#^tmp_table_size.*#tmp_table_size = 128M#" /etc/my.cnf
        sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 1024M#" /etc/my.cnf
        sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 256M#" /etc/my.cnf
        sed -i "s#^performance_schema_max_table_instances.*#performance_schema_max_table_instances = 6000#" /etc/my.cnf
    elif [[ ${MemTotal} -ge 16384 && ${MemTotal} -lt 32768 ]]; then
        sed -i "s#^key_buffer_size.*#key_buffer_size = 512M#" /etc/my.cnf
        sed -i "s#^table_open_cache.*#table_open_cache = 2048#" /etc/my.cnf
        sed -i "s#^sort_buffer_size.*#sort_buffer_size = 8M#" /etc/my.cnf
        sed -i "s#^read_buffer_size.*#read_buffer_size = 8M#" /etc/my.cnf
        sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 128M#" /etc/my.cnf
        sed -i "s#^thread_cache_size.*#thread_cache_size = 256#" /etc/my.cnf
        sed -i "s#^query_cache_size.*#query_cache_size = 256M#" /etc/my.cnf
        sed -i "s#^tmp_table_size.*#tmp_table_size = 256M#" /etc/my.cnf
        sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 2048M#" /etc/my.cnf
        sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 512M#" /etc/my.cnf
        sed -i "s#^performance_schema_max_table_instances.*#performance_schema_max_table_instances = 8000#" /etc/my.cnf
    elif [[ ${MemTotal} -ge 32768 ]]; then
        sed -i "s#^key_buffer_size.*#key_buffer_size = 1024M#" /etc/my.cnf
        sed -i "s#^table_open_cache.*#table_open_cache = 4096#" /etc/my.cnf
        sed -i "s#^sort_buffer_size.*#sort_buffer_size = 16M#" /etc/my.cnf
        sed -i "s#^read_buffer_size.*#read_buffer_size = 16M#" /etc/my.cnf
        sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 256M#" /etc/my.cnf
        sed -i "s#^thread_cache_size.*#thread_cache_size = 512#" /etc/my.cnf
        sed -i "s#^query_cache_size.*#query_cache_size = 512M#" /etc/my.cnf
        sed -i "s#^tmp_table_size.*#tmp_table_size = 512M#" /etc/my.cnf
        sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 4096M#" /etc/my.cnf
        sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 1024M#" /etc/my.cnf
        sed -i "s#^performance_schema_max_table_instances.*#performance_schema_max_table_instances = 10000#" /etc/my.cnf
    fi
}

function UninstallMySQL() {
    # 开始卸载MySQL
    RedEcho "注意该方法将删除MySQL的所有数据并不可恢复"

}

#TODO 目前只制作MySQL 55以及56的就OK了
function Install55() {
    installPath=/usr/local/mysql
    dataPath=$installPath/var
    mysqlrootpwd=$1
    # 不作任何检查
    mysqlver=5.5.62
    # 5.5MySQL已经不作更新了，因此不建议安装
    MySQLCommonYum
    mkdir -p $installPath/run
    mkdir -p $dataPath
    wget -c https://downloads.mysql.com/archives/get/p/23/file/mysql-$mysqlver.tar.gz
    tar zxvf mysql-$mysqlver.tar.gz
    cd mysql-$mysqlver
    rm -rf /etc/my.cnf
    cmake -DCMAKE_INSTALL_PREFIX=$installPath -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1
    MakeInstall
    groupadd mysql
    useradd -s /sbin/nologin -M -g mysql mysql
    cat >/etc/my.cnf<<EOF
[client]
#password	= your_password
port		= 3306
socket		= ${installPath}/run/mysql.sock

[mysqld]
port		= 3306
socket		= ${installPath}/run/mysql.sock
datadir = ${dataPath}
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M

#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id	= 1
expire_logs_days = 10

default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = ${dataPath}
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = ${dataPath}
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

${MySQLMAOpt}
EOF
MySQLOptimize
YellowEcho "开始执行权限授权操作"
chown -R mysql:mysql $dataPath
chown -R mysql:mysql $installPath/run
$installPath/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=${installPath} --datadir=${dataPath} --user=mysql
chgrp -R mysql $installPath
cp $installPath/support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
    cat > /etc/ld.so.conf.d/mysql.conf<<EOF
/usr/local/mysql/lib
/usr/local/lib
EOF
ldconfig
YellowEcho "开始将可执行程序创建快捷方式"
    ln -sf $installPath/lib/mysql /usr/lib/mysql
    ln -sf $installPath/include/mysql /usr/include/mysql
        if [ -d "/proc/vz" ]; then
        ulimit -s unlimited
    fi
    YellowEcho "开始创建systemctl服务"
    if command -v systemctl >/dev/null 2>&1; then
        cat >/usr/lib/systemd/system/mysql.service<<EOF
[Unit]
Description=MySQL
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
ExecStart=${installPath}/support-files/mysql.server start
ExecReload=${installPath}/support-files/mysql.server restart
ExecStop=${installPath}/support-files/mysql.server stop

[Install]
WantedBy=multi-user.target

EOF
        systemctl daemon-reload
        systemctl enable mysql.service
    fi
    /etc/init.d/mysqld start
    ln -sf $installPath/bin/mysql /usr/bin/mysql
    ln -sf $installPath/bin/mysqldump /usr/bin/mysqldump
    ln -sf $installPath/bin/myisamchk /usr/bin/myisamchk
    ln -sf $installPath/bin/mysqld_safe /usr/bin/mysqld_safe
    ln -sf $installPath/bin/mysqlcheck /usr/bin/mysqlcheck
    YellowEcho "开始秘密修改"
    cat >/tmp/tmpmy.cnf<<EOF
    [client]
    user=root
    password=''
    socket		= ${installPath}/run/mysql.sock
EOF
    $installPath/bin/mysql --defaults-file=/tmp/tmpmy.cnf -e "UPDATE mysql.user SET Password=PASSWORD('${mysqlrootpwd}') WHERE User='root';delete from user where not (user='root') ;delete from user where user='root' and password='';drop database test;DROP USER ''@'%';flush privileges;"
    /etc/init.d/mysqld restart
}

function Install56() {
    # 检查内存是否大于1G
    YellowEcho "开始安装MySQL5.6"
}

function Install57() {
    # 检查内存是否大于2G
    YellowEcho "开始安装MySQL5.7"
}

function Install8() {
    # 检查内存是否大于4G
    YellowEcho "开始安装MySQL8"
}

mysqlpass=$(tr -dc '_A-Z#\-+=a-z(0-9%^>)]{<|' </dev/urandom | head -c 15; echo)
echo "MySQL Password:"$mysqlpass>>setup.log

if [ "$1" == "55" ]; then
    YellowEcho "正在安装MySQL5.5"
    Install55 $mysqlpass
elif [ "$1" == "56" ]; then
  YellowEcho "正在安装MySQL5.6"
  Install56
elif [ "$1" == "57" ]; then
  YellowEcho "正在安装MySQL5.7"
  Install57
elif [ "$1" == "8" ]; then
    YellowEcho "正在安装MySQL8"
    Install8
fi















